/**
 * 购买商品
 * @constructor
 */
function Commodity() {
}

Commodity.init = function () {
    getshop();
}
let Id;
let price;
let comfirmPay_wxJsApiParam = null;
//获得商品信息
function getshop() {
    let tab_content = "";
    $.ajax({
        url:"/Applet/shop_show",
        type:"post",
        contentType:"application/json",
        dataType:"json",
        success:function(data){
            if (data.code === "200") {
                $.each(data.data, function (k, cabinetitem) {
                    if(cabinetitem.isNum==1)
                    {
                        tab_content += "<a href=\"javascript:;\" onclick=\"ComfirmPay(" + cabinetitem.cabinetitemId + ",'" + cabinetitem.name + "'," + cabinetitem.price + ")\" class=\"weui-grid\">";
                    }
                    else{
                        tab_content += '<a href="javascript:;" class="weui-grid">';
                    }

                    tab_content += ' <div class="weui-grid__icon">';
                    tab_content += '<img  src='+cabinetitem.image+' alt=""/>';
                    tab_content += ' </div>';
                    tab_content += '<p class="weui-grid__label">' + cabinetitem.name + '</p>';
                    if(cabinetitem.isNum==1)
                    {
                        tab_content += '<p class="weui-grid__label">￥' + cabinetitem.price + '</p>';
                    }
                    else{
                        tab_content += '<p class="weui-grid__label">￥' + cabinetitem.price + '<font color=red>(无货)</font></p>';
                    }

                    tab_content += '</a>';

                });
                $("#saleHtml").html(tab_content);
            }
        }
    });
}

//支付确认
function ComfirmPay(id,name,money) {
    Id = id;
    price = money;
    $('#ComfirmPay_jsdialog_content').html(name+":￥"+money);
    $('#ComfirmPay_iosDialog1').fadeIn(200);
}
//取消
$('#ComfirmPay_dialogs').on('click', '.close', function () {
    $(this).parents('.js_dialog').fadeOut(200);
});
//确认
$('#ComfirmPay_dialogs').on('click', '.sure', function () {
    //发起支付
    $(this).parents('.js_dialog').fadeOut(200);
    ComfirmPay_wxPay(Id);
});
function ComfirmPay_wxPay(Id) {
    //保留订单 发起支付
    let pm = {
        cabinetitemId:Id,
        amount:price
    }
    $.ajax({
        url: '/pay/payment',
        type: 'POST',
        async: false,
        data:JSON.stringify(pm),
        contentType:"application/json",
        dataType: 'json',
        success: function (data) {
            comfirmPay_wxJsApiParam = data;
            if (typeof WeixinJSBridge == "undefined") {
                console.log("11111:");
                if (document.addEventListener) {
                    document.addEventListener('WeixinJSBridgeReady', ComfirmPay_jsApiCall, false);
                }
                else if (document.attachEvent) {
                    document.attachEvent('WeixinJSBridgeReady', ComfirmPay_jsApiCall);
                    document.attachEvent('onWeixinJSBridgeReady', ComfirmPay_jsApiCall);
                }
            }
            else {
                ComfirmPay_jsApiCall();
            }


        }
    });
}
//调用微信JS api 支付
function ComfirmPay_jsApiCall() {
    console.log("comfirmPay_wxJsApiParam:",comfirmPay_wxJsApiParam);
    var jsonObj = JSON.parse(comfirmPay_wxJsApiParam);
    WeixinJSBridge.invoke('getBrandWCPayRequest', {
            "appId":comfirmPay_wxJsApiParam.appId, //公众号名称，由商户传入
            "timeStamp":comfirmPay_wxJsApiParam.timeStamp, //时间戳，自1970年以来的秒数
            "nonceStr":comfirmPay_wxJsApiParam.nonceStr, //随机串
            "package":comfirmPay_wxJsApiParam.packageValue,
            "signType":comfirmPay_wxJsApiParam.signType, //微信签名方式：
            "paySign":comfirmPay_wxJsApiParam.sign //微信签名
        }, function (res) {
            WeixinJSBridge.log(res.err_msg);
            // alert(res.err_code + res.err_desc + res.err_msg);
            //  alert(res.err_msg);
            if (res.err_msg == "get_brand_wcpay_request:ok" || res.err_msg == "ok") {
                //提示 支付成功提示
                $('#ComfirmPay_js_appointIcon').attr('class', 'weui-icon-success-no-circle weui-icon_toast');
                $('#ComfirmPay_js_appointContent').html("支付成功");
                $('#ComfirmPay_js_appoint').fadeIn(100);
                setTimeout(function () {
                    $('#ComfirmPay_js_appoint').fadeOut(100);
                }, 2000);
                //跳转到预约记录

            }
            else {
                $('#ComfirmPay_js_appointIcon').attr('class', 'weui-icon-warn weui-icon_toast');
                $('#ComfirmPay_js_appointContent').html("支付取消");
                $('#ComfirmPay_js_appoint').fadeIn(100);
                setTimeout(function () {
                    $('#ComfirmPay_js_appoint').fadeOut(100);
                }, 2000);


            }
        }
    );
}